package net.begincode.servlet.manage;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import utils.BQStringUtil;

import com.google.gson.Gson;

import net.begincode.bean.BcCourse;
import net.begincode.bean.BcUser;
import net.begincode.bean.CourseChapter;
import net.begincode.servlet.common.BaseServlet;

public class ManageServlet extends BaseServlet{
	private static final Log LOG = LogFactory.getLog(ManageServlet.class);
	
	/**
	 * 跳转到后台管理登录界面
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void manageInit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.getRequestDispatcher("/WEB-INF/manage/login.jsp").forward(request, response);
	}
	
	/**
	 * 用户登录
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
			String email = request.getParameter("email");
			String pwd = request.getParameter("pwd");
			BcUser bcUser = this.bcUserService.selUserByEmail(email);
			Gson gson = new Gson();
			Map msgMap = new HashMap();
			if(bcUser != null){
				bcUser.setHashPwd(pwd);
				if(BQStringUtil.makeMD5(pwd).equals(bcUser.getPwd())){
					try {
						this.addUserCookie(bcUser, "0", response);
						request.getRequestDispatcher("/WEB-INF/manage/manage.jsp").forward(request, response);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						LOG.equals("cookie设置失败");
					}
				}else{
					request.getRequestDispatcher("/WEB-INF/manage/login.jsp").forward(request, response);
				}				
			}else{
				request.getRequestDispatcher("/WEB-INF/manage/login.jsp").forward(request, response);
			}

	}
	/**
	 * 用户退出
	 * @throws IOException 
	 * @throws ServletException 
	 * */
	public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
			this.delCookieInfo(request, response);
			request.getRequestDispatcher("/WEB-INF/manage/login.jsp").forward(request, response);
	}




 
	
}
